<template>
  <div class="app-container"  v-loading="appLoading">
    <!-- 高级查询 -->
   <!-- <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
   --> <!--  <el-form-item label="店铺:">
        <el-input v-model="queryParams.storeName" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="汇总年月:">
        <el-input v-model="queryParams.jobMonth" size="small" placeholder="请输入汇总年月" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="SKU编码:">
         <el-input v-model="queryParams.cargoObjectNo" size="small" placeholder="请输入SKU编码" clearable @keyup.enter.native="handleQuery" />
       </el-form-item> -->
     <!-- <el-form-item label="商品名称:">
        <el-input v-model="queryParams.cargoObjectName" size="small" placeholder="请输入物料名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item> -->
    <!-- </el-form> -->

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['accounting:accApSkuMonthlySettlement:update']" type="success" icon="el-icon-edit" size="mini"  @click="handleAdd">新建汇总</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['accounting:accApSkuMonthlySettlement:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>

      <!-- <el-col :span="1.5">
        <el-button v-hasPermission="['accounting:accApSkuMonthlySettlement:update']" type="success" icon="el-icon-edit" size="mini"  @click="handleGenarate">生成汇总</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button v-hasPermission="['accounting:accApSkuMonthlySettlement:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

      <!-- <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
   -->  </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="汇总人" align="center" prop="operatorName" />
      <el-table-column label="汇总时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="汇总参数" align="center" prop="param" />
      <el-table-column label="操作人员IP" align="center" prop="ip" />
      <el-table-column label="操作类型" align="center" prop="operationType" />
      <!-- <el-table-column label="店铺" align="center" prop="storeName" />
       <el-table-column label="汇总年月" align="center" prop="jobMonth" />
       <el-table-column label="汇总结算数量" align="center" prop="wtCnt" />
      <el-table-column label="SKU编码" align="center" prop="cargoObjectNo" />
       <el-table-column label="物料名称" align="center" prop="cargoObjectName" />
       <el-table-column label="金额" align="center" prop="amount" /> -->
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
         <template slot-scope="scope"> -->
      <!--  <el-button v-hasPermission="['accounting:accApSkuMonthlySettlement:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
        <el-button v-hasPermission="['accounting:accApSkuMonthlySettlement:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
      --><!-- </template>
      </el-table-column> -->
    </el-table>

    <!-- 分页 -->

    <!-- 添加/修改对话框 -->
    <el-dialog :title="accApSkuMonthlySettlementFormDialog.title" :visible.sync="accApSkuMonthlySettlementFormDialog.visible" :close-on-click-modal="false" :before-close="resetAccApSkuMonthlySettlementFormDialog" width="500px" append-to-body>
      <div v-loading="appLoading">
        <el-form ref="accApSkuMonthlySettlementForm" :model="accApSkuMonthlySettlementFormData" :rules="accApSkuMonthlySettlementFormRules" label-width="80px">
          <el-form-item label="商城:">
            <el-select v-model="accApSkuMonthlySettlementFormData.mallId" placeholder="请选择商城编号" clearable size="small">
              <el-option v-for="option in options.mallOptions" :key="option.id" :label="option.customerName" :value="option.id" />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="店铺:">
             <el-select v-model="accApSkuMonthlySettlementFormData.storeId" placeholder="请选择店铺编号" clearable size="small">
               <el-option v-for="option in options.storeOptions" :key="option.storeCode" :label="option.storeName" :value="option.storeCode" />
             </el-select>
           </el-form-item> -->
          <el-form-item label="汇总年月:">
            <el-select v-model="accApSkuMonthlySettlementFormData.jobMonth" filterable clearable>
              <el-option v-for="item in options.jobMonthList" :key="item.key" :label="item.label" :value="item.key" />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="confirmAccApSkuMonthlySettlementFormDialog">确 定</el-button>
          <el-button @click="resetAccApSkuMonthlySettlementFormDialog">取 消</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import AccApSkuMonthlySettlementApi from '@/api/accounting/AccApSkuMonthlySettlementApi'
import OperationLogApi from '@/api/system/OperationLogApi'
import StoreApi from '@/api/basic/StoreApi'
import CustomerApi from '@/api/basic/CustomerApi'
const defaultAccApSkuMonthlySettlementFormData = {
  jobMonth: null, 
  storeId: "",
  mallId:"1353968310689075200"
}
export default {
  name: 'AccApSkuMonthlySettlementList',
  data() {
    return {
      options: {
        mallOptions: [],
        storeOptions: [],
        jobMonthList: this.$constant.jobMonthOptions,
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        storeId: null,
        mallId: null,
        status: null,
        jobMonth: null
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      accApSkuMonthlySettlementFormDialog: {
        title: '',
        visible: false
      },
      accApSkuMonthlySettlementFormData: Object.assign({}, defaultAccApSkuMonthlySettlementFormData),
      accApSkuMonthlySettlementFormRules: {
      },
      loading: true,
      appLoading: false,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
      CustomerApi.list().then(response => {
        this.options.mallOptions = response.data
      })
      StoreApi.list().then(response => {
        this.options.storeOptions = response.data
      })
    },
    handleAdd() {
      this.openAccApSkuMonthlySettlementFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return AccApSkuMonthlySettlementApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openAccApSkuMonthlySettlementFormDialog(id)
    },
    handleGenarate() {
      this.openGenDialog()
    },

    handleQuery() {
      /* this.loading = true
      AccApSkuMonthlySettlementApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      }) */
      this.loading = true
      this.queryParams.title = "财务月度sku汇总";
      OperationLogApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return AccApSkuMonthlySettlementApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    openAccApSkuMonthlySettlementFormDialog(id) {
      this.accApSkuMonthlySettlementFormDialog.visible = true
      if (id) {
        this.accApSkuMonthlySettlementFormDialog.title = '财务月度sku汇总-修改'
        AccApSkuMonthlySettlementApi.get(id).then(response => {
          this.accApSkuMonthlySettlementFormData = {
            ...this.accApSkuMonthlySettlementFormData,
            ...response.data
          }
        })
      } else {
        this.accApSkuMonthlySettlementFormDialog.title = '财务月度sku汇总-添加'
      }
    },
    confirmAccApSkuMonthlySettlementFormDialog() {
      this.$refs['accApSkuMonthlySettlementForm'].validate(valid => {
        if (valid) {
          this.appLoading = true
          if (this.accApSkuMonthlySettlementFormData.id) {
            AccApSkuMonthlySettlementApi.edit(this.accApSkuMonthlySettlementFormData).then(response => {
              this.appLoading = false
              this.accApSkuMonthlySettlementFormDialog.visible = false
              this.$message.success('修改成功')
              this.resetAccApSkuMonthlySettlementFormDialog()
              this.handleQuery()
            }).catch(error => {
              console.log(error)
              this.$message.error('修改失败')
              this.appLoading = false
            })
          } else {
            AccApSkuMonthlySettlementApi.add(this.accApSkuMonthlySettlementFormData).then(response => {
              this.appLoading = false
              this.accApSkuMonthlySettlementFormDialog.visible = false
              this.$message.success('添加成功')
              this.resetAccApSkuMonthlySettlementFormDialog()
              this.handleQuery()
            }).catch(error => {
              console.log(error)
              this.$message.error('添加失败')
              this.appLoading = false
            })
          }
        }
      })
    },
    resetAccApSkuMonthlySettlementFormDialog() {
      this.accApSkuMonthlySettlementFormDialog = {
        title: '',
        visible: false
      }
      this.accApSkuMonthlySettlementFormData = Object.assign({}, defaultAccApSkuMonthlySettlementFormData)
      this.resetForm('accApSkuMonthlySettlementForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
